package soar.test.profile;

import java.util.Enumeration;
import java.util.Properties;
import java.io.InputStream;
import java.util.ResourceBundle;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;

public class App
{
    public static void main(String[] args)
    {
        new App().run();
    }

    private void run()
    {
        try
        {
            String split = "==================================================";
            System.out.println(split);

            System.out.println(
                    "Object.class.getResourceAsStream(\"/config.properties\"): ");
            System.out.println();
            InputStream in = Object.class
                    .getResourceAsStream("/config.properties");
            Properties props = new Properties();
            props.load(in);
            props.entrySet().forEach(x -> {
                System.out.println(
                        String.format("%s = %s", x.getKey(), x.getValue()));
            });
            System.out.println(split);

            System.out.println("ResourceBundle.getBundle(\"config\"): ");
            System.out.println();
            ResourceBundle rb = ResourceBundle.getBundle("config");
            for (Enumeration<String> e = rb.getKeys(); e.hasMoreElements();)
            {
                String k = e.nextElement();
                System.out
                        .println(String.format("%s = %s", k, rb.getString(k)));
            }
            System.out.println(split);

            System.out.println(
                    "Object.class.getResourceAsStream(\"/config.yaml\"): ");
            System.out.println();
            Constructor constructor = new Constructor(Config.class);
            Yaml yaml = new Yaml(constructor);
            in = Object.class.getResourceAsStream("/config.yaml");
            Config config = (Config) yaml.load(in);
            in.close();
            System.out.println("mysql: ");
            config.getMysql().entrySet().forEach(x -> {
                System.out.println(String.format("    %s: ", x.getKey()));
                MySQL mysql = x.getValue();
                System.out.println(
                        String.format("        url: %s", mysql.getUrl()));
                System.out.println(String.format("        username: %s",
                        mysql.getUsername()));
                System.out.println(String.format("        password: %s",
                        mysql.getPassword()));
            });
            System.out.println(split);
        }
        catch (Exception e)
        {
            System.err.println(String.format("fail: %s", e));
        }
    }
}
